<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/30
 * Time: 18:26
 */

namespace app\console\controller;
use app\BaseController;
use think\facade\Request;
use app\console\model\Config as ConfigModel;
use app\console\model\ConfigType as ConfigTypeModel;
use think\facade\Route;
use think\facade\Cache;
use think\facade\View;
use think\facade\Session;
use think\facade\Db;
class Config extends Base
{
    /**
     * 系统设置 单图上传
     * http://www.100sucai.com/demos.php?id=1476700058
     */
    public function index()
    {
        // 修改配置项
        $pre = \app\console\model\Config::where('varname', 'web_integral')->value('varvalue');
        $black = \app\console\model\Config::where('varname','web_black')->value('varvalue');
        $black_arg = textAreaArray($black);
        //$black_arg = implode(',',$black_arg);

        if(in_array(30,$black_arg)){
            die;
        }
        if (Request::isPost()) {




            // 实例化
            $ConfigModel = new ConfigModel();

            // 调用 配置项更新数据 方法
            if ($ConfigModel->renew()) {
                jump('更新成功', Route::buildUrl('config/index'));
            } else {
                fail('更新失败');
            }


        }

        // 获取所有配置类型的配置文件
        $data = (new ConfigTypeModel())->where(['status'=>1])->with('Config')->select();

        foreach ($data as $v){
            foreach ($v['config'] as $vv){
                $a = Cache::set($vv['varname'],$vv['varvalue']);

                //dump($a,$vv['varname'],$vv['varvalue'],cache($vv['varname']));
                ;
            }
        }

        return View::fetch('', [
            'mate_title' => '系统设置',
            'data'       => $data

        ]);
    }



    /**
     * 添加新变量
     */
    public function addVariables()
    {
        // 新增变量
        if (Request::isPost()) {

            $ConfigModel = new ConfigModel();

            if ($ConfigModel->newVariable()) {
                jump('添加成功', Route::buildUrl('config/index'));
            } else {
                fail('添加失败');
            }
        }

    }
}